package tags_api

import (
	"gin_study_blog/common"
	"gin_study_blog/common/http"
	"gin_study_blog/models"
	"github.com/gin-gonic/gin"
)

type TagsRequest struct {
	Title string `json:"title" binding:"required" msg:"请输入标签"` // 标题
}

// TagsCreateView 标签添加
func (TagsApi) TagsCreateView(c *gin.Context) {
	var cr TagsRequest
	// 二选一
	//err := c.ShouldBindBodyWith(&cr, binding.JSON)
	err := c.ShouldBindJSON(&cr)
	if err != nil {
		http.FailWithError(err, &cr, c)
		return
	}
	// 判断重复
	var tags models.TagModel
	err = common.DB.Take(&tags, "title = ?", cr.Title).Error
	if err == nil {
		http.FailWithMessage("标签名重复", c)
		return
	}

	tagsData := &models.TagModel{
		Title: cr.Title,
	}

	err = common.DB.Create(tagsData).Error
	if err != nil {
		common.Log.Error(err)
		http.FailWithMessage("添加失败", c)
		return
	}
	http.IsOkWithMessage("添加标签成功", c)
}
